/*
 * 项目组:
 *            = = =      =       =      =       =       =
 *          =            =       =       =     = =     =
 *         =             = = = = =        =   =   =   =
 *          =            =       =         = =     = =
 *            = = =      =       =          =       =
 */

function query(keyword){
    $.ajax({
        url:"/book/book/findDoc",
        dataType:"json",
        data: {
            "keyWord":keyword
        },
        type:"post",
        success:function(data){
            $("#booklist").empty();
            for (const result of data) {
                let imgurl = result.imgurl;
                if (imgurl.indexOf("http" )=== -1&&imgurl.indexOf("https" )=== -1){
                    imgurl = "http://localhost:9001/manager"+imgurl;
                }
                let page = '<li>' +
                    '<div class = "cover">' +
                    '<a href = "/book/book/bookdetails/'+result.id+'" target = "_blank" >' +
                    '<img src = "'+imgurl+'" >' +
                    '</a>' +
                    '</div>' +
                    ' <div class="info">' +
                    ' <h2 class="name">' +
                    '<a href="/book/book/bookdetails/'+result.id+'" target="_blank">'+result.bookname+'</a>' +
                    '</h2>' +
                    '<div class="otherInfo">' +
                    '<span class="author">'+result.author+'</span>' +
                    ' <i>/</i>' +
                    '<span class="pulishTiem">'+result.publishdate+'</span>' +
                    '<i>/</i>' +
                    '<span class="publisher">'+result.pressname+'</span>' +
                    '</div>' +
                    '<div class="priceWrap">' +
                    '<span class="sellPrice">¥'+result.price+'</span>' +
                    ' <span class="discount">(10.0折)</span>' +
                    ' <span class="priceTit">定价:</span>' +
                    ' <del class="">¥'+result.price+'</del>' +
                    ' </div>' +
                    '<p class="recoLagu">' +result.bookintroduce +
                    '</p>' +
                    ' <div class="oparateButton">' +
                    '<span class="buyButton">加入购物车</span>' +
                    '</div></div></li>';
                let _page = $(page);
                _page.appendTo($("#booklist"));
            }
        }
    })
}


$(function(){

    let url =  location.search;
    if(url.indexOf("?") !== -1){
        let keyword = decodeURIComponent(url.substring(1).split("=")[1]);
        if (keyword != null){
            $("#search-box").val(keyword);
            query(keyword);
        }
    }else{
        $("#search-box").val("");
        query("");
    }

    $("#search-btn").click(function () {
        query($("#search-box").val());
    })



    $("#bl-seach-menu li").click(function(){
        $(this).parent().find("li").removeClass("bl-active");
        $(this).addClass("bl-active");

        
    });
    $("#toggleTypeBtn").click(function(){
        $("#toggleTypeList").toggleClass("hidden");
        $(this).toggleClass("text");
        $(this).find("span").toggleClass("am-icon-angle-down")
                            .toggleClass("am-icon-angle-up");  
    });

    $("#bookSort li").click(function(){
        // 效果事件 不要修改
        $("#bookSort li").removeClass("on");
        $(this).addClass("on");
        $("#bookSort li:not(.on) span").removeClass("up").removeClass("down");
        var $span = $(this).find("span");
        if($span.length==0){
            return;
        }
        var classStr = $span[0].className;
        if(classStr.indexOf("up")==-1&&classStr.indexOf("down")==-1){
            $span.addClass("up");
            return;
        }
        $span.toggleClass("up");
        $span.toggleClass("down");
    }).click(function(){
        // 数据事件 参数依赖于 页面中得 data-sort 属性
        var sort = $(this).find("i").attr("data-sort");
        if($(this).find("span").length!=0){
            var isAsc = $(this).find("span")[0].className.indexOf("up") != -1;
            var isDesc = $(this).find("span")[0].className.indexOf("down") != -1;
        }
        console.log("查询方式："+sort+"，升序："+isAsc+"，降序："+isDesc);
        // 根据条件获取数据 并渲染页面
    });

    
    $("body").on('click',".buyButton",function () {
        let href = $(this).parent().parent().siblings(".cover").children("a").attr("href");
        let array = href.split("/");
        let id = array[array.length-1];
        $.ajax({
            url:"/book/book/shoppingCar/"+id,
            dataType:"json",
            data: {
                "id":id
            },
            type:"post",
            success:function(data){
                alert(data.msg);
            }
    })
    })

});